import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.*;
import java.awt.Robot;
import java.awt.event.KeyEvent;

public class loginPanel extends JPanel implements ActionListener
{
	
//Defines all variables, input fields, and buttons ====================================================================
	JButton submitButton;
	JTextField classField;
	JLabel classLabel, instructions;
	Timer programTimer;
	
	String classNo;
	String instruction;
	char[] classArray = new char[6];
	
	public loginPanel(){

//Creates all of the input fields and buttons =========================================================================
instruction = "<html>INSTRUCTIONS<br><br>1.Log into eLion<br><br>2.Goto the drop/add page<br><br>3.Select the semester you wish to schedule for<br><br>4.Enter the course number in the box above<br><br>5.Click the submit button<br><br>6.Switch back to the eLion webpage and place the cursor in one the 'schedule No'. boxes (note: you have 10 seconds to do this before the program begins executing)<br><br>7.Leave your computer running and don't touch anything until you want it to stop<br><br><hr>*currently only works for classes where there are still open sections</html>";
		submitButton = new JButton("Submit");
		classField = new JTextField();
		classLabel = new JLabel("Enter Course Number");
		instructions = new JLabel(instruction);
		
		programTimer = new Timer(30000, this);
		
		setLayout(null);
//Add and setbounds for all inputs fields and buttons =================================================================		

		add(classField);
		add(classLabel);
		add(submitButton);
		add(instructions);
	
		classLabel.setBounds(10,10,140,25);
		classField.setBounds(150,10,130,25);
		submitButton.setBounds(10,50,280,25);
		instructions.setBounds(10,85,280,400);
				
		submitButton.addActionListener(this);
	}
	
//Action Listener ================================================================================================
	public void actionPerformed(ActionEvent event) {
    	Object obj = event.getSource();
//Converts the class number to an array and fills out the initial forms===========================================
      	if(obj == submitButton){
			classNo = classField.getText();
			classNo.getChars(0, 6, classArray, 0);
			try{
				Robot robot = new Robot();
				robot.delay(10000);
				for(int i=0; i<6; i++){					
					if(classArray[i]=='0'){robot.keyPress(KeyEvent.VK_0);}
					if(classArray[i]=='1'){robot.keyPress(KeyEvent.VK_1);}
					if(classArray[i]=='2'){robot.keyPress(KeyEvent.VK_2);}
					if(classArray[i]=='3'){robot.keyPress(KeyEvent.VK_3);}
					if(classArray[i]=='4'){robot.keyPress(KeyEvent.VK_4);}
					if(classArray[i]=='5'){robot.keyPress(KeyEvent.VK_5);}
					if(classArray[i]=='6'){robot.keyPress(KeyEvent.VK_6);}
					if(classArray[i]=='7'){robot.keyPress(KeyEvent.VK_7);}
					if(classArray[i]=='8'){robot.keyPress(KeyEvent.VK_8);}
					if(classArray[i]=='9'){robot.keyPress(KeyEvent.VK_9);}
				}
				robot.keyPress(KeyEvent.VK_ENTER);	
        	    robot.delay(15000);
        	    robot.keyPress(KeyEvent.VK_TAB);
				robot.keyPress(KeyEvent.VK_TAB);
				robot.keyPress(KeyEvent.VK_TAB);
				robot.keyPress(KeyEvent.VK_TAB);
				for(int i=0; i<6; i++){					
					if(classArray[i]=='0'){robot.keyPress(KeyEvent.VK_0);}
					if(classArray[i]=='1'){robot.keyPress(KeyEvent.VK_1);}
					if(classArray[i]=='2'){robot.keyPress(KeyEvent.VK_2);}
					if(classArray[i]=='3'){robot.keyPress(KeyEvent.VK_3);}
					if(classArray[i]=='4'){robot.keyPress(KeyEvent.VK_4);}
					if(classArray[i]=='5'){robot.keyPress(KeyEvent.VK_5);}
					if(classArray[i]=='6'){robot.keyPress(KeyEvent.VK_6);}
					if(classArray[i]=='7'){robot.keyPress(KeyEvent.VK_7);}
					if(classArray[i]=='8'){robot.keyPress(KeyEvent.VK_8);}
					if(classArray[i]=='9'){robot.keyPress(KeyEvent.VK_9);}
				}
				robot.keyPress(KeyEvent.VK_ENTER);
				
			}catch (AWTException e) {
            e.printStackTrace();
        	}
        	programTimer.start();
		}
//Main task that continually tries to schedule the class ===========================================================		
		if(obj == programTimer){				
			try{
				Robot robot = new Robot();
				robot.keyPress(KeyEvent.VK_TAB);
				robot.keyPress(KeyEvent.VK_TAB);
				robot.keyPress(KeyEvent.VK_TAB);
				robot.keyPress(KeyEvent.VK_ENTER);
				robot.delay(15000);
				robot.keyPress(KeyEvent.VK_TAB);
				robot.keyPress(KeyEvent.VK_TAB);
				robot.keyPress(KeyEvent.VK_TAB);
								for(int i=0; i<6; i++){					
					if(classArray[i]=='0'){robot.keyPress(KeyEvent.VK_0);}
					if(classArray[i]=='1'){robot.keyPress(KeyEvent.VK_1);}
					if(classArray[i]=='2'){robot.keyPress(KeyEvent.VK_2);}
					if(classArray[i]=='3'){robot.keyPress(KeyEvent.VK_3);}
					if(classArray[i]=='4'){robot.keyPress(KeyEvent.VK_4);}
					if(classArray[i]=='5'){robot.keyPress(KeyEvent.VK_5);}
					if(classArray[i]=='6'){robot.keyPress(KeyEvent.VK_6);}
					if(classArray[i]=='7'){robot.keyPress(KeyEvent.VK_7);}
					if(classArray[i]=='8'){robot.keyPress(KeyEvent.VK_8);}
					if(classArray[i]=='9'){robot.keyPress(KeyEvent.VK_9);}
				}
				robot.keyPress(KeyEvent.VK_ENTER);
        	    } catch (AWTException e) {
            e.printStackTrace();
        	}
      	}
	}	
}